﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Mysqlx.Crud;

namespace BookMS
{
    public partial class admin3 : Form
    {
        public admin3()
        {
            InitializeComponent();
            Table();
        }

        public void Table()
        {
            dataGridView1.Rows.Clear();  // 清空旧数据
            Dao dao = new Dao();
            string sql = "select uid, bid, bname, borrowtime, returntime from lend";
            IDataReader dc = dao.read(sql);

            while (dc.Read())
            {
                // 尝试将 returntime 字段转换为 DateTime
                DateTime returnTime;
                DateTime.TryParse(dc[4].ToString(), out returnTime);
                DateTime currentTime = DateTime.Now;

                // 添加行到 DataGridView
                int rowIndex = dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());

                // 如果 returnTime 早于当前时间，则表示已逾期
                if (returnTime < currentTime)
                {
                    dataGridView1.Rows[rowIndex].DefaultCellStyle.ForeColor = Color.Red;  // 设置字体颜色为红色
                }
            }

            dc.Close();
            dao.DaoClose();
        }


        private void button5_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();  //清空旧数据
            Dao dao = new Dao();
            string sql = $"select * from lend where bid='{textBox1.Text}'";
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            dataGridView1.Rows.Clear();  //清空旧数据
            Dao dao = new Dao();
            string sql = $"select * from lend where bname='{textBox2.Text}'";
            IDataReader dc = dao.read(sql);
            while (dc.Read())
            {
                dataGridView1.Rows.Add(dc[0].ToString(), dc[1].ToString(), dc[2].ToString(), dc[3].ToString(), dc[4].ToString());
            }
            dc.Close();
            dao.DaoClose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string id = textBox3.Text;
            string title = textBox4.Text;
            string content = textBox5.Text;

            Dao dao = new Dao();
            string sql = $"insert into message(senderid, receiverid, title, content, sendtime) values ('1', '{id}', '{title}', '{content}', NOW())";
            if (dao.Execute(sql) > 0)
            {
                MessageBox.Show("通知成功");
            }
            else
            {
                MessageBox.Show("通知失败！");
            }
            dao.DaoClose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Table();
        }
    }
}
